/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package testing;
import lime2.*;
import java.io.*;

/**
 * This example illustrates the functionalities of Launcher.
 * <br>
 * Optional user input: tuplespace adapter to test.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini 
 */

public class ExtLaunching 
{
 /**
  * Main method.
  * 
  * @param args user input: (optional) name of the tuplespace adapter to test
  */	
 public static void main(String[] args)
 {
  // aux variable	
  int uport = 1980;		
  String tsfactory;	
  
  // if the user did not choose any tuplespace factory then we use the default tuplespace factory
  if (args != null && args.length != 1) tsfactory = "lights.adapters.builtin.TupleSpaceFactory";  
                                   else tsfactory = "lights.adapters." + args[0] + ".TupleSpaceFactory";
 	 	  	
  // initializes settings 	
  Launcher.getServerSettings().setProperty(Launcher.UCASTPORT_KEY, new Integer(uport).toString());	
  Launcher.getServerSettings().setProperty(Launcher.TSFACTORY_KEY, tsfactory);	
  
  // starts the server	
  if (!Launcher.serverLaunched()) Launcher.launchServer(); 
  
  // loads agent "testing.Dummy" (local mode, no parameters)
  try { Launcher.launchAgent("testing.Dummy"); }
  catch (AgentCreationException ace) { ace.printStackTrace(); System.exit(-1); }
 
  // loads agent "testing.Dummy" (local mode, parameters)
  try { Launcher.launchAgent("testing.Dummy", new Object[] {"dummyspace"}); }
  catch (AgentCreationException ace) { ace.printStackTrace(); System.exit(-1); }
  
  // loads agent "testing.Out" (forces remote mode, no parameters)
  try { Launcher.launchRemoteAgent("testing.Out", "golem", uport + 1); }
  catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); }
  
  // loads agent "testing.Out" (forces remote mode, parameters)
  try { Launcher.launchRemoteAgent("testing.Out", new Serializable[] {"i am a tuple"}, "golem", uport + 1); }
  catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); }

  // loads multiple agents, "testing.Rdp" and "testing.Inp" (still uses remode mode)
  Launcher.launch("-load testing.Rdp -load testing.Inp"); 
  
  // loads an agent that loads another agent (still uses remote mode)
  Launcher.launch(new String[] {"-load", "testing.LoadAgent"});
 }
	
}
